package com.conversationboard.controller.subscriptions;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.SecurityChecks;
import com.conversationboard.model.User;

@WebServlet(name = "UnsubscribeControllerServlet", urlPatterns = "/UnsubscribeControllerServlet")
public class UnsubscribeControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -8368471469765022865L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			if (SecurityChecks.passes(request, user, this)) {
				int boardId = Integer.parseInt(request.getParameter("boardid"));
				user.unsubscribe(boardId);
			}

			RequestDispatcher dispatcher = request.getRequestDispatcher("/PrePreferencesControllerServlet");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
